<template>
    <!--    <div>载体</div>-->
    <div class="app-container">
        <el-row :gutter="10" class="mb8" ref="refbut">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
            </el-col>
        </el-row>

        <el-table ref="table" v-loading="loading" :data="dataList" :height="tableHeight"
            @selection-change="handleSelectionChange">
            <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="ztId" />
             <el-table-column label="保存路径" prop="savePath" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="载体名称" prop="name" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span v-for="(i, idx) in dict.type.CARRIER_TYPE" :key="idx">
                            {{ scope.row.name == i.value ? i.label : '' }}
                        </span>
                </template>
            </el-table-column>
            <el-table-column label="载体编号" prop="code" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="载体大小" prop="size" align="center">
                <template slot-scope="scope">
                    <span>{{ scope.row.size + 'MB' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="离线存储位置" prop="offlineSavePath" align="center" :show-overflow-tooltip="true" /> 
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button plain type="warning" size="mini" @click="handleDetection(scope.row)"
                       >检测</el-button>
                    <el-button plain type="success" size="mini" @click="handleUpdate(scope.row)"
                       >修改</el-button>
                    <el-button plain type="danger" size="mini" @click="handleDelete(scope.row)"
                       >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 载体添加及修改 -->
        <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body :before-close="cancelForm">
            <div>
                <el-form ref="form" :rules="rules" :model="form" label-width="120px">
                    <el-form-item label="保存路径" prop="savePath">
                        <el-input v-model="form.savePath" placeholder="请输入保存路径"></el-input>
                    </el-form-item>
                    <el-form-item label="载体类型" prop="name">
                        <el-select v-model="form.name" placeholder="请选择" clearable style="width: 100%">
                            <el-option v-for="item in dict.type.CARRIER_TYPE" :key="item.value" :label="item.label" :value="item.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="载体编号" prop="code">
                        <el-input v-model="form.code" placeholder="请输入载体编号"></el-input>
                    </el-form-item>
                    <el-form-item label="载体大小" prop="size">
                        <el-input-number v-model="form.size" controls-position="right" placeholder="请输入" :min="1"
                            style="width: 88%;"></el-input-number> (MB)
                    </el-form-item>
                    <el-form-item label="离线存储位置" prop="offlineSavePath">
                        <el-input v-model="form.offlineSavePath" placeholder="请输入离线存储位置"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancelForm">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
    
<script>
import estimateTableHeight from "@/mixins/estimateTableHeight";
import { getTableList,addItem,editItem,yqCarrierManage,yqdelete,detection} from "@/api/cqbc/resourceManagement/yqCarrierManage.js"


export default {
    mixins: [estimateTableHeight], // 混入方法
    archive_Dicts: ['CARRIER_TYPE'],
    data() {
        const checkFieldLength = (rule, value, callback) => {
            if (value) {
                if (value.length > 127) {
                    callback(new Error('输入位数超过127位,请重新输入'))
                } else {
                    callback()
                }
            } else {
                callback()
            }
        }
        return {
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            dataList: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },

            title: '',
            open: false,
            // 表单参数
            form: {
                name: undefined,
                code: undefined,
                savePath: undefined,
                offlineSavePath: undefined,
                size: undefined
            },
            ztlxNameOption: [],
            // 表单校验
            rules: {
                name: [
                    { required: true, message: '请输入载体类型', trigger: 'blur' },
                ],
                code: [
                    { required: true, message: '请输入载体编号', trigger: 'blur' },
                ],
                savePath: [
                    { required: true, message: '请输入保存路径', trigger: 'blur' },
                ],
                offlineSavePath: [
                    { required: true, message: '请输入离线存储位置', trigger: 'blur' },
                    { required: true, trigger: 'blur', validator: checkFieldLength }
                ],
                size: [
                    { required: true, message: '请输入载体大小', trigger: 'blur' },
                ],
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            getTableList(this.queryParams).then(response => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        
        table_index(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.ztId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        reset() {
            this.form = {
                name: undefined,
                code: undefined,
                savePath: undefined,
                offlineSavePath: undefined,
                size: undefined
            }
        },
        // 新增
        handleAdd() {
            this.resetForm('form')
            this.reset()
            this.open = true
            this.title = '新增载体'
        },
        // 修改
        handleUpdate(row) {
            this.resetForm('form')
            this.reset()
            const id = row.id || this.ids
            yqCarrierManage(id).then(response => {
                this.form = response.data
                this.title = '修改载体'
                this.open = true
            })
        },
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != undefined) {
                        // this.form.ztlxName = this.ztlxNameOption.find(item => item.value == this.form.ztlxId).label
                        editItem(this.form).then(response => {
                            this.$modal.msgSuccess(response.msg)
                            this.open = false
                            this.getList()
                        })
                    } else {
                        // this.form.ztlxName = this.ztlxNameOption.find(item => item.value == this.form.ztlxId).label
                       
                        addItem(this.form).then(response => {
                            this.$modal.msgSuccess(response.msg)
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        cancelForm() {
            this.open = false
        },
        // 删除
        handleDelete(row) {
            const id = row.id || this.ids
            this.$modal.confirm('是否确认删除选中载体类型为"' + row.name + '"的数据项？').then(function () { }).then(() => {
                yqdelete(id).then(res => {
                    if (res.code == "200") {
                        this.$modal.msgSuccess("删除成功");
                        this.getList();
                    }
                })
            }).catch(() => { this.$modal.closeLoading(); });
        },

        //载体检测
        handleDetection(row){
            this.$modal.loading("加载中...")
            this.$modal.closeLoading();
            this.$modal.confirm('是否要进行检测').then(() => {
                detection({
                    code:row.code,
                    name:row.name,
                    savePath:row.savePath
                }).then(res => {
                    if (res.code == "200") {
                        this.$modal.closeLoading();
                        this.$modal.msgSuccess("检测成功");
                        this.getList();
                    }else{
                        this.$modal.closeLoading(); 
                    }
                }).catch(res=>{
                    this.$modal.closeLoading();
                })
            }).catch(() => { 
                this.$modal.closeLoading();
            });
        }
    }
}
</script>
    
<style></style>
    